require.config({baseUrl: '/js/lib'});
require(['zepto', 'lib'], function ($, lib) {
    var fn = {}, lib = new lib();
    $.extend(fn, {
        init: function () {
            fn.getnearbyShop();
            $(document).on('tap','#searchResult',function(){
                location.href = '/wine_shop.html?sId=' + $(this).data('sid');
            })
        },
        getnearbyShop: function () {
            $('#searchBtn').on('tap', function () {
                var param = {
                    'storageName': $('input').val()?$('input').val():'北京',
                    'lon': lib.getUrlParam('lon'),
                    'lat': lib.getUrlParam('lat')
                };
                lib.ajx(lib.getReq().ser + 'storage/searchName.action', param, function (data) {
                        if (data.infocode == '0') {
                            if(!data.info.length){
                                $('.ui-shop-list').html('');
                                lib.showMessage('暂无该餐厅信息，换个名字试试呢！');
                            }else{
                                var info = data.info;
                                var html = '';
                                for(var i in info){
                                    var distance = info[i].distance.toString();
                                    if (distance > 999) {
                                        distance = (distance.substring(0, distance.indexOf('.')) / 1000);
                                        var offset = 0;
                                        if (distance > 10 && distance <100) {
                                            offset = 1;
                                        } else if (distance > 100 && distance <1000) {
                                            offset = 0;
                                        } else if (distance > 1000 && distance <10000) {
                                            offset = -1;
                                        } else if (distance > 10000 && distance <100000) {
                                            offset = -2;
                                        }
                                        distance = distance.toString().substring(0, 3 - offset) + 'Km'
                                    } else {
                                        distance = distance.substring(0, distance.indexOf('.')) + 'm';
                                    }
                                    html += `<li id='searchResult' data-sid="${info[i].storageId}">
                                              <a href="#">
                                                <ul class="ui-shop-info">
                                                  <li><i class="ui-position"></i></li>
                                                  <li>
                                                    <p>${info[i].storageName}</p>
                                                    <p class="address-overflow">${info[i].address}</p>
                                                  </li>
                                                  <li style="top: 3px;">${distance}</li>
                                                </ul>
                                              </a>
                                            </li>`;
                                }
                                $('.ui-shop-list').html(html);
                            }
                        }
                    },
                    function () {
                        console.log('search.js => searchName.action fail!');
                    }
                )
            });
        }
    });
    fn.init();
});